package com.example.apartment.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 处理统一异常的handler
 *
 * @author wuzexin
 * @date 2021/10/28 14:02
 */

@ControllerAdvice
public class GlobalExceptionHandler {
    private final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Object handleException(Exception e) {
        // 打印错误信息
        log.error("Default Exception: ", e);
        return ApiRestResponse.error(RespExceptionEnum.SYSTEM_ERROR);
    }

    @ExceptionHandler(RespException.class)
    @ResponseBody
    public Object handleRespException(RespException e) {
        log.error("RespException: ", e);
        return ApiRestResponse.error(e.getCode(), e.getMsg());
    }

}
